package com.example.smartcity.ui.AcNews

import android.content.Context
import android.os.Bundle
import android.provider.Settings.Global
import android.text.Html
import android.view.MenuItem
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.smartcity.R
import com.example.smartcity.SmartCityApplication
import com.example.smartcity.SmartCityApplication.Companion.context
import com.example.smartcity.logic.net_work.Response
import com.example.smartcity.logic.utils.BaseActivity
import com.example.smartcity.logic.utils.Toast.Companion.showToast
import com.example.smartcity.ui.AcNews.adapter.NewSCommentSRecyclerViewAdapter
import com.example.smartcity.ui.AcNews.model.NewsComment
import com.example.smartcity.ui.home.HomeViewModel
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import org.json.JSONObject

class NewSFlashShowActivity : BaseActivity() {

    private val url = SmartCityApplication.url

    private val newsViewModel by lazy {
        ViewModelProvider(this)[HomeViewModel::class.java]
    }

    /**
     * 新闻列表
     */
    private val comment by lazy {
        ViewModelProvider(this)[NewSFlashShowActivityViewModel::class.java]
    }

    private lateinit var newSTitle: TextView
    private lateinit var newSPublishDate: TextView
    private lateinit var newSImageView: ImageView
    private lateinit var newSContend: TextView
    private lateinit var newsReadNum: TextView
    private lateinit var newsLikeNum: TextView
    private lateinit var newsCommentNum: TextView
    private lateinit var newsEditText: EditText
    private lateinit var newsEditTextOk: Button

    // 新闻评论列表
    private lateinit var newSCommentRecyclerView: RecyclerView

    // 适配器
    private lateinit var adapter: NewSCommentSRecyclerViewAdapter

    // 新闻id临时存储
    var newsId: Int? = null

    // 存储新闻评论数据
    private val adapterArray = ArrayList<NewsComment>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_sflash_show3)
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        title = "新闻文章详细"

        newSTitle = findViewById(R.id.title)
        newSImageView = findViewById(R.id.imageView)
        newSContend = findViewById(R.id.contend)
        newSPublishDate = findViewById(R.id.publishDate)
        newsReadNum = findViewById(R.id.newsReadNum)
        newsLikeNum = findViewById(R.id.newsLikeNum)
        newsCommentNum = findViewById(R.id.newsCommentNum)
        newSCommentRecyclerView = findViewById(R.id.NewScommentRecyclerView)

        // 发表评论控件
        newsEditText = findViewById(R.id.newsEditText)
        newsEditTextOk = findViewById(R.id.newsEditTextOK)

        val data = intent.getStringExtra("model")
        if (data != null) positionIndex(data)

        val hot = intent.getStringExtra("hot")
        if(hot != null) hotTitle(hot)

        // 发表新闻评论
        if (hot != null) addComment(hot)

    }

    /**
     * 发表新闻评论内容
     */
    private fun addComment(hot: String) {
        newsEditTextOk.setOnClickListener {
            if (newsEditText.text.isEmpty()) "评论内容不能为空！".showToast()
            else {

            }
        }
    }


    /**
     * 根据id进行显示新闻内容
     */
    private fun positionIndex(data: String) {
        newsViewModel.newS(9)
        newsViewModel.newSLiveData.observe(this, Observer { referrer ->
            val body = referrer.getOrNull()
            if(body != null){
                newsViewModel.newSList.clear()
                newsViewModel.newSList.addAll(body)
            }

            for(i in newsViewModel.newSList){
                if (data.isEmpty()){
                    Toast.makeText(context, "加载轮播图失败！", Toast.LENGTH_SHORT).show()
                }else{
                    if(i.id == 29) if(data == "0") {
                        // 根据id查找评论
                        setComment(i.id)

                        newSTitle.text = i.title
                        val news = "发布日期: ${i.publishDate} · 来自智慧城市 · 新闻总台"
                        newSPublishDate.text = news
                        Glide.with(this)
                            .load(SmartCityApplication.url + i.cover)
                            .into(newSImageView)
                        newSContend.text = Html.fromHtml(i.content)
                        // 阅读量·点赞·评论
                        newsReadNum.text = i.readNum.toString()
                        newsLikeNum.text = i.likeNum.toString()
                        newsCommentNum.text = i.commentNum.toString()
                    }

                    if(i.id == 31) if(data == "1") {
                        // 根据id查找评论
                        setComment(i.id)

                        newSTitle.text = i.title
                        val news = "发布日期: ${i.publishDate} · 来自智慧城市 · 新闻总台"
                        newSPublishDate.text = news
                        Glide.with(this)
                            .load(SmartCityApplication.url + i.cover)
                            .into(newSImageView)
                        newSContend.text = Html.fromHtml(i.content)
                        // 阅读量·点赞·评论
                        newsReadNum.text = i.readNum.toString()
                        newsLikeNum.text = i.likeNum.toString()
                        newsCommentNum.text = i.commentNum.toString()
                    }

                    if(i.id == 32) if(data == "2") {
                        // 根据id查找评论
                        setComment(i.id)

                        newSTitle.text = i.title
                        val news = "发布日期: ${i.publishDate} · 来自智慧城市 · 新闻总台"
                        newSPublishDate.text = news
                        Glide.with(this)
                            .load(SmartCityApplication.url + i.cover)
                            .into(newSImageView)
                        newSContend.text = Html.fromHtml(i.content)
                        // 阅读量·点赞·评论
                        newsReadNum.text = i.readNum.toString()
                        newsLikeNum.text = i.likeNum.toString()
                        newsCommentNum.text = i.commentNum.toString()
                    }
                }
            }


        })
    }

    /**
     * 根据标题显示内容
     */
    private fun hotTitle(hot: String){
        newsViewModel.newsAll("测试")
        newsViewModel.newSLiveDataAll.observe(this, Observer { resources ->
            val body = resources.getOrNull()
            if(body != null){
                newsViewModel.newsAll.clear()
                newsViewModel.newsAll.addAll(body)
            }

            for (i in newsViewModel.newsAll){
                if (hot == i.title){
                    // 根据id查找评论
                    setComment(i.id)

                    newSTitle.text = i.title
                    val news = "发布日期: ${i.publishDate} · 来自智慧城市 · 新闻总台"
                    newSPublishDate.text = news
                    Glide.with(this).load(SmartCityApplication.url + i.cover).into(newSImageView)
                    newSContend.text = Html.fromHtml(i.content)
                    newsReadNum.text = i.readNum.toString()
                    newsLikeNum.text = i.likeNum.toString()
                    newsCommentNum.text = i.commentNum.toString()
                }
            }
        })
    }

    /**
     * 根据id显示评论列表内容
     */
    private fun setComment(id: Int) {
        "加载评论中...".showToast()
        // 创建适配器
        val layout = LinearLayoutManager(this)
        newSCommentRecyclerView.layoutManager = layout
        adapter = NewSCommentSRecyclerViewAdapter(adapterArray)
        newSCommentRecyclerView.adapter = adapter


        comment.setComments(id)
        comment.newsCommentLiveData.observe(this, Observer { resources ->
            val body = resources.getOrNull()
            if(body != null){
                comment.newsCommentList.clear()
                comment.newsCommentList.addAll(body)
            }

            // 设置数据
            for (i in comment.newsCommentList){
                if (i.newsId == id){
                    if (adapterArray.size <= 300){
                        adapterArray.add(NewsComment(
                            i.userName,
                            i.content,
                            i.commentData
                        ))
                        // 刷新数据
                        adapter.notifyItemChanged(adapterArray.size)
                    }
                }
            }
            "加载成功...".showToast()
        })


    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // 销毁该页面
        this.finish()
        return super.onOptionsItemSelected(item)
    }
}